package com.geekcattle.service.goods.iml;

import com.geekcattle.mapper.goods.GoodsMapper;
import com.geekcattle.service.goods.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: geek-framework
 * @description:
 * @author: LQ
 * @create: 2019-04-19 23:17
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 获取所有的商品
     * 分页查询所有商品和所有商品的种类
     * 需要支持多条件查询
     * 需要支持按照商品类型查询
     *
     * @param map
     * @return
     */
    @Override
    public List<Map> getAllGoods(Map map) {
        return null;
    }

    /**
     * 获取所有商品类型
     * 和类型下的所有商品
     * @param map
     * @return
     */
    @Override
    public List<Map> getGoodsType(Map map) {
        List<Map> goodsTypeList = goodsMapper.getGoodsType(map);
        return (List<Map>)this.getTree(goodsTypeList,map);
    }

    @Override
    public List<Map> getGoods(Map map) {
        return goodsMapper.getGoods2(map);
    }

    /**
     * 树形结构转换
     * @param listMap
     * @return
     */
    public Object getTree(List<Map> listMap,Map map) {
        if (listMap == null) {
            return null;
        }
        Iterator<Map> iterator = listMap.iterator();
        while (iterator.hasNext()) {
            Map next = iterator.next();
            if (next.get("id") == null) {
                continue;
            }
            if (map != null){
                if (map.get("goods_name") != null){
                    next.put("goods_name",map.get("goods_name"));
                }
            }
            next.put("goods",goodsMapper.getGoods(next));
            List<Map> goodsTypeChild = goodsMapper.getGoodsType(next);
            if (goodsTypeChild.size() <= 0){
                continue;
            }
            next.put("child",goodsTypeChild);
            this.getTree(goodsTypeChild,map);
        }
        return listMap;
    }
}

